# -*- coding: utf-8 -*-
# @Time : 2024/1/20 20:38
# @Author : cute
# @Email : Aggressive_cute@126.com
import functools
from sanic.response import text
from sanic.request import Request
from sanic import Blueprint, json
from .douyin.core import dy_helper
from settings.server_settings import white_list

dy_api = Blueprint('dy_api', url_prefix='/dy_api')

# 验签
def verify_signature(func):
    invalid_info = 'Invalid Signature 请联系管理员(v):  boboteacher8888'
    @functools.wraps(func)
    def wrapper(*args, **kw):
        request = args[0]
        signature = request.headers.get('signature')
        if not signature:
            return text(invalid_info, status=403)

        if signature not in white_list:  # 白名单
            return text(invalid_info, status=403)

        return func(*args, **kw)

    return wrapper


# /cute_api/dy_api/slide
@dy_api.route("/slide", methods=["POST", "GET"])
# @verify_signature
async def dy_score(request):
    """POST JSON{fp/ detail}"""
    data_dict = None
    if request.method != "POST":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return json(data_dict)
    try:
        client_dict = request.json
        if "detail" in client_dict:
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": dy_helper.pass_slide(client_dict.get('detail'),
                                             client_dict.get('fp'))
            }
        else:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": "请传入参数: detail"
            }
            return json(data_dict)


    except Exception as e:
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": str(e)
        }

    return json(data_dict)


# 抖音滑块轨迹
@dy_api.route("/slide/tracks", methods=["POST", "GET"])
@verify_signature
async def dy_tracks(request):
    data_dict = None
    if request.method != "POST":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return json(data_dict)
    try:
        client_dict = request.json
        data_dict = {
            "code": 0,
            "msg": "success",
            "data": {
                'tracks': dy_helper.get_tracks(**client_dict)
            }
        }


    except Exception as e:
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": str(e)
        }

    return json(data_dict)


# 抖音商品详情
@dy_api.route("/promotion", methods=["POST", "GET"])
@verify_signature  # 验证签名
async def dy_promotion(request):
    data_dict = None
    if request.method != "POST":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return json(data_dict)
    try:
        client_dict = request.json
        if "promotion_ids" in client_dict:
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": dy_helper.get_shop_detail(**client_dict)
            }
        else:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": "请传入参数: promotion_ids"
            }
            return json(data_dict)


    except Exception as e:
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": str(e)
        }

    return json(data_dict)
